/*
 * @Author: zhangyujie
 * @Date:   2015-08-12 10:44:47
 * @Last Modified by:   zhangyujie
 * @Last Modified time: 2015-08-13 13:12:12
 */



define(['jquery', 'd3'], function($, d3) {
	function list() {
		var setting = {
			selector: '#three_2',			
			title: {
				selector: '#title',
				text: ['资产总规模(万元)', '预期总收益(万元)'],
				subtext: [],
				textStyle: {
					'margin-top': '-7px',
					'margin-bottom': '3px',
					'font-size': '14px',
					'color': '#8E8E8F',
					'text-align': 'center',
					'font-famliy': '微软雅黑'
				},
				subtextStyle: {
					'margin': 0,
					'font-size': '20px',
					'color': '#8E8E8F',
					'font-weight': 'bold',
					'text-align': 'center',
					'font-famliy': '微软雅黑'
				}

			},
			series: {
				category: ['投资', '收益金额'],
				dataset: [{
					'name': '郁金香25号基金(0002300)',
					'value': [300, 28.5],
					'rate': 0.095
				}, {
					'name': '郁金香23号基金(0002330)',
					'value': [200, 18],
					'rate': 0.09
				}, {
					'name': '郁金香31号基金(0005310)',
					'value': [100, 8.5],
					'rate': 0.085
				}, {
					'name': '郁金香43号基金(0003330)',
					'value': [50, 2.45],
					'rate': 0.049
				}, {
					'name': '郁金香52号基金(0002530)',
					'value': [100, 62],
					'rate': 0.062
				}, {
					'name': '郁金香21号基金(0002210)',
					'value': [50, 10],
					'rate': 0.20
				}, {
					'name': '郁金香37号基金(0002303)',
					'value': [20, 1.54],
					'rate': 0.077
				}],
				textStyle: {
					"font-size": '16px',
					'font-weight': 'bold',
					'fill': '#858586',
					'font-famliy': '微软雅黑'
				}
			},
			total: {
				x1: 12,
				y1: 12,
				x2: 250,
				y2: 12,
				x3: 290,
				y3: 12
			},
			income: {
				x1: 12,
				y1: 0,
				x2: 215,
				y2: 0,
				x3: 290,
				y3: 0
			},
			circle: {
				r: 5,
				cx: 5,
				cy: 5
			},
			otherColor: ['#F9F9F9', '#fff', '#EFEFEF'],
			colorset: ['#24D2FD', '#60AFF9', '#1989BB', '#2CB551', '#E6E747', '#F6B86D', '#FD8C84'],
			margin: {
				top: 30,
				right: 10,
				bottom: 10,
				left: 5
			}
		}
		three_2(setting)

		function three_2(setting) {

			var selector = setting.selector,
				series = setting.series,
				category = series.category,
				dataset = series.dataset,
				title = setting.title,
				text = title.text,
				subtext = title.subtext,
				textStyle = series.textStyle,
				titleTextStyle = title.textStyle,
				subtitleTextStyle = title.subtextStyle,
				totalSetting = setting.total,
				incomeSetting = setting.income,
				circleSetting = setting.circle,
				data = [],
				total = [],
				income = [],
				otherColor = setting.otherColor,
				colorset = setting.colorset,
				margin = setting.margin,
				width = document.querySelector(selector).offsetWidth,
				height = document.querySelector(selector).offsetHeight,
				length = dataset.length,
				rectHeight = height / length

			var svg,
				content,
				totalGroup,
				rectScale,
				incomeGroup,
				circle

			
			for (var i = 0, tempData; tempData = dataset[i++];) {
				total.push(tempData['value'][0])
				income.push(tempData['value'][1])
			}
			data = [d3.sum(total), d3.sum(income).toFixed(2)]
			d3.select(title.selector).selectAll('div')
				.each(function(d, i) {
					d3.select(this).select('dt').html(text[i]).style(titleTextStyle)
					d3.select(this).select('dd').html(data[i]).style(subtitleTextStyle)
				})
			rectScale = d3.scale.linear()
				.domain([0, 1])
				.range([0, 70])

			svg = d3.select(selector).append('svg')
				.attr('width', width)
				.attr('height', height)

			content = svg.append('g')
				.attr('class', '.content')

			var categoryG = content.selectAll('.categoryG')
				.data(dataset)
				.enter()
				.append('g')
				.attr('class', 'categoryG')
				.attr('transform', function(d, i) {
					return 'translate(' + 0 + ',' + i * rectHeight + ')'
				})

			categoryG.append('rect')
				.attr('x', 0)
				.attr('y', 0)
				.attr('width', width)
				.attr('height', rectHeight)
				.attr('fill', function(d, i) {
					return i % 2 == 0 ? otherColor[0] : otherColor[1]
				})
			content.select('.categoryG')
				.append('line')
				.attr('x1', 0)
				.attr('y1', 0)
				.attr('x2', width)
				.attr('y2', 0)
				.attr('fill', 'none')
				.attr('stroke', otherColor[2])
				.attr('stroke-width', '2')

			categoryG.append('line')
				.attr('x1', 0)
				.attr('y1', function(d, i) {
					return rectHeight
				})
				.attr('x2', width)
				.attr('y2', function(d, i) {
					return rectHeight
				})
				.attr('fill', 'none')
				.attr('stroke', otherColor[2])
				.attr('stroke-width', '2')

			totalGroup = categoryG.append('g')
				.attr('class', 'totalGroup')
				.attr('transform', function(d, i) {
					return 'translate(' + margin.left + ',' + margin.top + ')'
				})

			totalGroup.append('circle')
				.attr('r', circleSetting['r'])
				.attr('cx', circleSetting['cx'])
				.attr('cy', circleSetting['cy'])
				.attr('fill', function(d, i) {
					return colorset[i]
				})
			totalGroup.append('text')
				.text(function(d, i) {
					return d['name']
				})
				.attr('x', totalSetting['x1'])
				.attr('y', totalSetting['y1'])
				.style(textStyle)
			totalGroup.append('text')
				.text(function(d, i) {
					return category[0] + ' '
				})
				.attr('x', totalSetting['x2'])
				.attr('y', totalSetting['y2'])
				.style(textStyle)
			totalGroup.append('text')
				.text(function(d, i) {
					return d['value'][0] + '万'
				})
				.attr('x', totalSetting['x3'])
				.attr('y', totalSetting['y3'])
				.style(textStyle)
			incomeGroup = categoryG.append('g')
				.attr('class', 'incomeGroup')
				.attr('transform', function(d, i) {
					return 'translate(' + margin.left + ',' + (margin.top + 40) + ')'
				})

			incomeGroup.append('text')
				.text(function(d, i) {
					return '预期收益率 ' + (d['rate'] * 100).toFixed(1) + '%'
				})
				.attr('x', incomeSetting['x1'])
				.attr('y', incomeSetting['y1'])
				.style(textStyle)

			incomeGroup.append('rect')
				.attr('x', 145)
				.attr('y', -11)
				.attr('width', function(d, i) {
					return rectScale(d['rate'])
				})
				.attr('height', 13)
				.attr('fill', function(d, i) {
					return colorset[i]
				})

			incomeGroup.append('text')
				.text(function(d, i) {
					return category[1] + ' '
				})
				.attr('x', incomeSetting['x2'])
				.attr('y', incomeSetting['y2'])
				.style(textStyle)
			incomeGroup.append('text')
				.text(function(d, i) {
					return +d['value'][1] + '万'
				})
				.attr('x', incomeSetting['x3'])
				.attr('y', incomeSetting['y3'])
				.style(textStyle)

		}
	}

	return {
        list : list
    }
})